package net.ipinglun.module.newsDetail;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.ipinglun.R;
import net.ipinglun.module.main.MainActivity;

import android.widget.Toast;


import com.gc.materialdesign.views.Switch;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;

import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;


public class NewsDetailActivity extends SwipeBackActivity {


    private String mNewsId;
    private NewsDetailPresenter mPresenter;
    private ListView mListView;
    private RelativeLayout mHeaderView;
    private RelativeLayout mFooterView;
    private WebView mWebView;
    private TextView mNewTitle;

    private WindowManager mWindowManager;
    private View mView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);


        Bundle bundle = this.getIntent().getExtras();
        mNewsId = bundle.getString("id");

        mPresenter = new NewsDetailPresenter();

        mListView = (ListView)findViewById(R.id.list_view);

        mHeaderView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.news_detail_header, null);
        mWebView = (WebView) mHeaderView.findViewById(R.id.web_view);
        mNewTitle = (TextView) mHeaderView.findViewById(R.id.new_title);
        mListView.addHeaderView(mHeaderView);

        mFooterView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.waterfall_footer, null);
        mListView.addFooterView(mFooterView);

        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));

        getNewsDetail();
    }

    public void getNewsDetail() {

        mPresenter.getNewsDetail(mNewsId, new GetNewsDetailResponseHandler() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(NewsDetailBean newsDetail) {
                mNewTitle.setText(newsDetail.title);
                mWebView.loadDataWithBaseURL("", newsDetail.content, "text/html", "utf-8", "");
            }

            @Override
            public void onFailure() {
            }

            @Override
            public void onFinish() {


            }

        });
    }

    public void clickFinish(View v) {
        super.finish();
    }

    private List<String> getData() {
        List<String> data = new ArrayList<String>();
        data.add("测试数据1");
        data.add("测试数据2");
        data.add("测试数据3");
        data.add("测试数据4");
        return data;
    }


    @Override
    public void onStop() {
        super.onStop();

        mPresenter.cancelRequest();
    }


    public void clickShare(View v) {

        Dialog.Builder builder = null;

        builder = new DatePickerDialog.Builder(R.style.Material_App_Dialog_DatePicker_Light){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                DatePickerDialog dialog = (DatePickerDialog)fragment.getDialog();
                String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
                Toast.makeText(NewsDetailActivity.this, "Date is " + date, Toast.LENGTH_SHORT).show();
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                Toast.makeText(NewsDetailActivity.this, "Cancelled" , Toast.LENGTH_SHORT).show();
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.positiveAction("OK")
                .negativeAction("CANCEL");

        DialogFragment fragment = DialogFragment.newInstance(builder);
        FragmentManager fm = getSupportFragmentManager();
        fragment.show(fm, null);

    }

    public void clickSetting(View v) {

        View contentView = getLayoutInflater().inflate(R.layout.toolbar_setting, null);

        Switch nightSwitch = (Switch)contentView.findViewById(R.id.night_switch);


        DiscreteSeekBar seekBar = (DiscreteSeekBar)contentView.findViewById(R.id.seek_bar);

        seekBar.setNumericTransformer(new DiscreteSeekBar.NumericTransformer() {
            @Override
            public int transform(int value) {

                System.out.println("DiscreteSeekBar=" + value);
                return value;
            }
        });


        final DialogPlus dialog = new DialogPlus.Builder(this)
                .setContentHolder(new ViewHolder(contentView))
                .setCancelable(true)
                .setGravity(Gravity.CENTER)
                .create();
        dialog.show();
    }


    @Override
    protected void onDestroy() {

        // 不移除会有一堆警告
        //mWindowManager.removeViewImmediate(mView);

        super.onDestroy();

    }

}
